package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IComSearchAdDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.subclass.displayBean.comSearchAd.ComSearchAdExtendBean;
import com.wsj.web.vo.homeSite.ComSearchAd;

public class ComSearchAdDaoImpl extends HibernateDaoSupport implements IComSearchAdDao {

	public void add(ComSearchAd comSearchAd) throws DataAccessException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(comSearchAd);

	}

	public void delete(Integer id) throws DataAccessException {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().delete(this.getHibernateTemplate().get(ComSearchAd.class, id));
		

	}

	public PageResultSet loadComSearchAdByExtendBean(ComSearchAdExtendBean comSearchAdExtendBean)
			throws DataAccessException {
		// TODO Auto-generated method stub
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from ComSearchAd as m where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(comSearchAdExtendBean.getPage(), comSearchAdExtendBean.getPageSize());
		
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        
        //标题
        if(comSearchAdExtendBean.getTitle()!= null&&!comSearchAdExtendBean.getTitle().equals("")){
            stringBuffer.append(" and m.title like ?");
            
            list.add("%"+comSearchAdExtendBean.getTitle()+"%");
        }
        //关键字
        if(comSearchAdExtendBean.getKeyword()!= null&&!comSearchAdExtendBean.getKeyword().equals("")){
        	stringBuffer.append(" and m.keyword like ?");
        	
        	list.add("%"+comSearchAdExtendBean.getKeyword()+"%");
        }
        //回收站
        if(comSearchAdExtendBean.getDel()!= null&&comSearchAdExtendBean.getDel().intValue()!=-2){
        	stringBuffer.append(" and m.del = ?");
        	
        	list.add(comSearchAdExtendBean.getDel());
        }
        //类别
        if(comSearchAdExtendBean.getComAdTypeid()!= null&&comSearchAdExtendBean.getComAdTypeid().intValue()!=-2){
        	stringBuffer.append(" and m.comAdType.id = ?");
        	
        	list.add(comSearchAdExtendBean.getComAdTypeid());
        }
        //审核
        if(comSearchAdExtendBean.getAudite()!= null&&comSearchAdExtendBean.getAudite().intValue()!=-2){
        	stringBuffer.append(" and m.audite = ?");
        	
        	list.add(comSearchAdExtendBean.getAudite());
        }
        //是否图片
        if(comSearchAdExtendBean.getIsPicAd()!= null&&comSearchAdExtendBean.getIsPicAd().intValue()!=-2){
        	stringBuffer.append(" and m.isPicAd = ?");
        	
        	list.add(comSearchAdExtendBean.getIsPicAd());
        }
        //整理时间
        if(comSearchAdExtendBean.getBeginDate()!=null&&!comSearchAdExtendBean.getBeginDate().equals("")
        		&&comSearchAdExtendBean.getEndDate()!=null&&!comSearchAdExtendBean.getEndDate().equals("")){
        	
        	stringBuffer.append(" and m.createTime >= ? and m.createTime<= ? ");
        	
        	list.add(comSearchAdExtendBean.getBeginDate().trim()+" 00:00:00");
        	list.add(comSearchAdExtendBean.getEndDate().trim()+" 23:59:59");
        }
        
		stringBuffer.append(" order by m.id desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);

		return pageResultSet;
	}

	public ComSearchAd loadComSearchAdById(Integer id)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
		ComSearchAd comSearchAd=(ComSearchAd) this.getHibernateTemplate().get(ComSearchAd.class, id);
		
		return comSearchAd;
	}

	public void update(ComSearchAd comSearchAd) throws DataAccessException {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().update(comSearchAd);

	}

	public List loadComSearchAdBycomAdType(String typeName, int top) throws DataAccessException {
		// TODO Auto-generated method stub
		String hql=" select h  from  ComSearchAd as h where  h.comAdType.typeName = ? and h.audite = ? and h.del = ? order by createTime desc";
		Query q=this.getSession().createQuery(hql);
		q.setParameter(0, typeName);
		q.setParameter(1,new Short("1"));
		q.setParameter(2, new Short("0"));
		q.setFirstResult(0);
		q.setMaxResults(top);
		return q.list();

	}

}
